<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">数据列表</span>
      <el-button class="btn-add" @click="handleAddMenu()" size="mini">添加</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="menuTable"
        style="width: 100%"
        :data="list"
        row-key="id"
        :indent="32"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        v-loading="listLoading"
        border
      >
        <!-- <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>-->
        <el-table-column label="菜单名称" align="left">
          <template slot-scope="scope">{{scope.row.title}}</template>
        </el-table-column>
        <el-table-column label="菜单级数" width="100" align="center">
          <template slot-scope="scope">{{scope.row.level | levelFilter}}</template>
        </el-table-column>
        <el-table-column label="前端名称" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="前端图标" width="100" align="center">
          <template slot-scope="scope">
            <svg-icon :icon-class="scope.row.icon"></svg-icon>
          </template>
        </el-table-column>
        <el-table-column label="是否显示" width="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleHiddenChange(scope.$index, scope.row)"
              :active-value="0"
              :inactive-value="1"
              v-model="scope.row.hidden"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template slot-scope="scope">{{scope.row.sort }}</template>
        </el-table-column>
        <!-- <el-table-column label="设置" width="120" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              :disabled="scope.row.level | disableNextLevel"
              @click="handleShowNextLevel(scope.$index, scope.row)">查看下级
            </el-button>
          </template>
        </el-table-column>-->
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleUpdate(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <!-- <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>-->
    </div>

    <el-dialog
      :title="!isupdate ? '添加' : '编辑'"
      :visible.sync="isshowdia"
      width="30%"
      @close="isshowdia = false;$refs.menudetailcom.ismenushow = false;"
    >
      <MenuDetail ref="menudetailcom" />

      <!-- <span slot="footer">
        <el-button @click="isshowdia = false;$refs.menudetailcom.ismenushow = false;">取 消</el-button>
        <el-button type="primary" @click="1">确 定</el-button>
      </span>-->
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchList,
  deleteMenu,
  updateMenu,
  updateHidden,
  fetchTreeList
} from "@/api/menu";

import MenuDetail from "./components/MenuDetail";

export default {
  name: "menuList",
  components: {
    MenuDetail
  },
  data() {
    return {
      isupdate: false,
      isshowdia: false,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 5
      },
      parentId: 0
    };
  },
  created() {
    this.resetParentId();
    this.getList();
  },
  watch: {
    $route(route) {
      this.resetParentId();
      this.getList();
    }
  },
  methods: {
    resetParentId() {
      this.listQuery.pageNum = 1;
      if (this.$route.query.parentId != null) {
        this.parentId = this.$route.query.parentId;
      } else {
        this.parentId = 0;
      }
    },
    handleAddMenu() {
      // this.$router.push('/permissions/addMenu');

      this.isupdate = false;
      this.isshowdia = true;
      this.$nextTick(function() {
        this.$refs.menudetailcom.isEdit = false;
        this.$refs.menudetailcom.ismenushow = true;
      });
    },
    getList() {
      this.listLoading = true;
      // fetchList(this.parentId, this.listQuery).then(response => {
      //   this.listLoading = false;
      //   this.list = response.data.list;
      //   this.total = response.data.total;
      // });
      fetchTreeList().then(response => {
        this.listLoading = false;
        this.list = response.data;
        this.total = 100;
      });
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleHiddenChange(index, row) {
      updateHidden(row.id, { hidden: row.hidden }).then(response => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000
        });
      });
    },
    handleShowNextLevel(index, row) {
      // this.$router.push({path: '/permissions/menu', query: {parentId: row.id}})
    },
    handleUpdate(index, row) {
      // this.$router.push({path:'/permissions/updateMenu',query:{id:row.id}});
      this.$root.permission.editmenu = row;
      this.isupdate = true;
      this.isshowdia = true;

      this.$nextTick(function() {
        this.$refs.menudetailcom.isEdit = true;
        this.$refs.menudetailcom.ismenushow = true;
      });
    },
    handleDelete(index, row) {
      deleteMenu(row.id).then(response => {
        this.$message({
          message: "删除成功",
          type: "success",
          duration: 1000
        });
        this.getList();
      });
    }
  },
  filters: {
    levelFilter(value) {
      let level = "";
      switch (value) {
        case 0:
          level = "一级";
          break;
        case 1:
          level = "二级";
          break;
        case 2:
          level = "三级";
          break;
        case 3:
          level = "四级";
          break;
        case 4:
          level = "五级";
          break;
        case 5:
          break;
        default:
          break;
      }
      return level;
    },
    disableNextLevel(value) {
      if (value === 0) {
        return false;
      } else {
        return true;
      }
    }
  }
};
</script>

<style scoped>
</style>
